angular.module('Controller')
.controller('QgyCotenantEditModal',($scope,urls,qgyApiService,$rootScope,$localStorage,NgTableParams,commonUtil,cryptUtil,wfAlert,commonConstant,$http, $uibModalInstance, items)->
    $scope.type=type=items.type
    $scope.data=items.data
    if type=='view'
        $scope.modalTitle='查看住客信息'
    else if type=='edit'
        $scope.modalTitle='编辑住客信息'
    else if type=='new'
        $scope.modalTitle='新增住客信息'
    $scope.modalSexList=commonConstant.numSexList()
    $scope.modalStatusList=commonConstant.cotenantStatusList()
    imageUpData=[
        {id:'#cotenant-idcard-front',paramName:'cardFront'}
        {id:'#cotenant-idcard-back',paramName:'cardBack'}
    ]
    $scope.uploadImageData=(eleId)->
        qgyApiService.bindImageUploadAndGenApi(eleId)
        $(eleId).click()
        ''
    $rootScope.$on('qgy-image-upload',(val,data)->
        return if imageUpData==undefined
        tmp=_.find(imageUpData,(item)->item.id==data.eleId)
        object=$scope.data
        if(tmp and object)
            object[tmp.paramName]=data.pictureUrl
    )
    _.extend $scope,
        newData:()->
            reqData={
                "contractId":$scope.data.contractId
                "customerName":$scope.data.customerName
                "customerCard":$scope.data.customerCard
                "customerPhone":$scope.data.customerPhone
                "cardBack": $scope.data.cardBack
                "cardFront": $scope.data.cardFront
                "job":$scope.data.job
                "sex": $scope.data.sex
                "nation": $scope.data.nation
                "address": $scope.data.address
                "status": $scope.data.status
            }
            $http.post("/cotenant",reqData)
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo($scope.modalTitle+'成功')
                    $uibModalInstance.close('done')
                else
                    wfAlert.toastError(resp.message)
            )
        editData:()->
            reqData={
                "customerName":$scope.data.customerName
                "customerCard":$scope.data.customerCard
                "customerPhone":$scope.data.customerPhone
                "cardBack": $scope.data.cardBack
                "cardFront": $scope.data.cardFront
                "job":$scope.data.job
                "sex": $scope.data.sex
                "nation": $scope.data.nation
                "address": $scope.data.address
                "status": $scope.data.status
            }
            $http.put("/cotenant/#{$scope.data.id}",reqData)
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo($scope.modalTitle+'成功')
                    $uibModalInstance.close('done')
                else
                    wfAlert.toastError(resp.message)
            )
    $scope.confirm = ()->
        if type=='edit'
            $scope.editData()
        else if type=='new'
            $scope.newData()
        else
            $uibModalInstance.dismiss('cancel')
    $scope.cancel =()->
        $uibModalInstance.dismiss('cancel')
)